#include "udpServer.hpp"
using namespace Server;
#include <memory>
#include <unordered_map>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fstream>
#include <signal.h>

// const string filepath = "./myfile.txt";

// unordered_map<string, string> dick;

static void Uasge()
{
    cout << "Usage\n\t"
         << "local_port error" << endl;
}


static void handlemessage(const int sockfd, const string &clientip, const uint16_t &clientport, const string &message)
{
    // 在这里对业务逻辑进行处理,在返回回去
    string response_message; // 返回的字符串函数
    // 开始返回
    struct sockaddr_in respose;
    bzero(&respose, sizeof(respose)); // 记得清空
    respose.sin_family = AF_INET;
    respose.sin_port = htons(clientport);
    respose.sin_addr.s_addr = inet_addr(clientip.c_str());

    response_message+=message+"[server toback]";

    sendto(sockfd, response_message.c_str(), response_message.size(), 0, (struct sockaddr *)&respose, sizeof(respose));
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Uasge();
        exit(USAGE_ERROR);
    }
    uint16_t serverport = atoi(argv[1]);
     unique_ptr<udpserver> se_ptr(new udpserver(handlemessage, serverport));  //翻译


    se_ptr->initServer();
    se_ptr->startServer();
    return 0;
}